package com.paiai.lslease.action.house;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.paiai.lslease.auto.model.AppLockOpenrecord;
import com.paiai.lslease.base.AppResult;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.model.bo.query.AppLockOpenrecordQuery;
import com.paiai.lslease.service.appuser.AppOpenRecordService;

/** 
* @author chenmfa
* @version 创建时间：2017年10月20日 上午10:17:25 
* @description 开门记录模块
*/
@SuppressWarnings("serial")
public class AppDoorRecordAction extends BaseAction{
  private static final Logger logger = LoggerFactory.getLogger(AppDoorRecordAction.class);
  
  private String mobile;
  private String mac;
  private String targetUser;
  private Integer page;
  private Integer rows;
  @Autowired
  private AppOpenRecordService appOpenRecordService;
  
  public void addOpenRecord(){
    AppResult result;
    if(null == mobile){
      result = AppResult.newFailResult("用户不能为空");
    }else{
      try{
        logger.info(request.getRequestURI()+",接收到的参数："+JSON.toJSONString(request.getParameterMap()));
        AppLockOpenrecord record = new AppLockOpenrecord();
        record.setOpenIdentify(mobile);
        //查询设备记录
        appOpenRecordService.addOpenDoorRecord(record,mac);
        result =AppResult.newSusscessResult("添加成功");
      }catch(Exception e){
        logger.error("添加失败",e);
        result =AppResult.newFailResult("添加失败");
      }
    }
    sendResultMsg(result);
  }
  
  /**
   * @description 查询某账号下的开门记录信息
   */
  public void queryUserDooorRecord(){
    AppResult result;
    if(null == mobile || null == mac){
      result = AppResult.newFailResult("用户或设备编号不能为空");
    }else{
      try{
        logger.info(request.getRequestURI()+",接收到的参数："+JSON.toJSONString(request.getParameterMap()));
        if(null == page || page.intValue()< 1){
          page = 1;
        }
        if(null == rows || rows.intValue() < 1){
          rows = 20;
        }
        AppLockOpenrecordQuery query = new AppLockOpenrecordQuery();
        query.setOpenIdentify(targetUser);
        query.setPageSize(rows);
        query.setStartIndex((page-1)*rows);
        query.setMobile(mobile);
        query.setMac(mac);
        result = appOpenRecordService.queryOpenDoorRecord(query);
      }catch(Exception e){
        logger.error("查询失败",e);
        result =AppResult.newFailResult("查询失败");
      }      
    }
    sendResultMsg(result);
  }

  public void setMobile(String mobile) {
    this.mobile = mobile;
  }
  public void setMac(String mac) {
    this.mac = mac;
  }
  public void setTargetUser(String targetUser) {
    this.targetUser = targetUser;
  }
  public void setPage(Integer page) {
    this.page = page;
  }
  public void setRows(Integer rows) {
    this.rows = rows;
  }
}
